import { InputProps } from "antd"
import FormInput from "../../../components/cell/FormInput"
import useIntentModel from "../../../model/useIntentModel"

export interface Props extends InputProps {
    updatekey: string
    additionalkeys?: [string, number]
    methodkey: 'updateCustomerInfo' | 'updateGuarantorInfo'
}

const InputCell = (props: Props) => {

    const { entryInfo, updateCustomerInfo, updateGuarantorInfo, updateListData } = useIntentModel()

    const groups = {
        "updateCustomerInfo": { method: updateCustomerInfo, data: entryInfo?.customer },
        "updateGuarantorInfo": { method: updateGuarantorInfo, data: entryInfo?.guarantorInfos },
    }

    const updateMethod = groups[props.methodkey].method
    let sourceData = groups[props.methodkey].data
    let updateAction = (v: any) => updateMethod({ [props.updatekey]: v })
    //Array<object>
    if (props.additionalkeys) {
        const key = props.additionalkeys[0]
        const index = props.additionalkeys[1]
        //value
        const obj = groups[props.methodkey].data || {}
        const arr = obj[key] || []
        sourceData = arr[index]
        updateAction = (v: any) => updateMethod({ [key]: updateListData(v, key, index, groups[props.methodkey].data, props.updatekey) })
    }
    console.log(entryInfo?.guarantorInfos)

    return <FormInput inputchange={updateAction} value={sourceData?.[props.updatekey]} {...props} />

}

export default InputCell